package fes.ui;

import com.ht.aems.fesmodel.AbstractUnit;
import com.ht.aems.fesmodel.CommUnit;
import com.ht.aems.fesmodel.IUnitManager;
import com.ht.aems.fesmodel.PointManager;
import com.ht.aems.fesmodel.data.AnalogPoint;
import com.ht.aems.fesmodel.data.DiscretePoint;
import com.ht.aems.fesmodel.data.MeasurePoint;
import com.ht.aems.swing.util.TreeNodeListener;
import com.ht.aems.swing.util.TreeUtil;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.util.*;

/**
 * util class help managing device
 *
 * @author Dong Shufeng
 * @version 1.0
 * @Date: 2006-6-1
 */
public class RTUTreeUtil {
    public static ImageIcon iconOfFather = new ImageIcon(RTUTreeUtil.class.getResource("/icons/substation1.png"));
    public static ImageIcon IconOfRTU = new ImageIcon(RTUTreeUtil.class.getResource("/icons/rtu1.png"));

    public static void addAnalogPointToTree(DefaultMutableTreeNode n) {
        TreeUtil.traversalTree_DFS(n, new TreeNodeListener() {
            public void actionPerformed(TreeNode treeNode) {
                if (treeNode.isLeaf() && (treeNode instanceof DefaultMutableTreeNode)) {
                    DefaultMutableTreeNode n = ((DefaultMutableTreeNode) treeNode);
                    Object obj = n.getUserObject();
                    if (obj != null && obj instanceof CommUnit) {
                        CommUnit u = (CommUnit) obj;
                        AnalogPoint[] points = PointManager.getInstance().getAnalogPoints(u.getId());
                        for (AnalogPoint p : points)
                            n.insert(new DefaultMutableTreeNode(p), n.getChildCount());
                    }
                }
            }
        });
    }

    public static void addDiscretePointToTree(DefaultMutableTreeNode n) {
        TreeUtil.traversalTree_DFS(n, new TreeNodeListener() {
            public void actionPerformed(TreeNode treeNode) {
                if (treeNode.isLeaf() && (treeNode instanceof DefaultMutableTreeNode)) {
                    DefaultMutableTreeNode n = ((DefaultMutableTreeNode) treeNode);
                    Object obj = n.getUserObject();
                    if (obj != null && obj instanceof CommUnit) {
                        CommUnit u = (CommUnit) obj;
                        DiscretePoint[] points = PointManager.getInstance().getDiscretePoints(u.getId());
                        for (DiscretePoint p : points)
                            n.insert(new DefaultMutableTreeNode(p), n.getChildCount());
                    }
                }
            }
        });
    }

    public static void addPointToTree(DefaultMutableTreeNode n) {
        TreeUtil.traversalTree_DFS(n, new TreeNodeListener() {
            public void actionPerformed(TreeNode treeNode) {
                if (treeNode.isLeaf() && (treeNode instanceof DefaultMutableTreeNode)) {
                    DefaultMutableTreeNode n = ((DefaultMutableTreeNode) treeNode);
                    Object obj = n.getUserObject();
                    if (obj != null && obj instanceof CommUnit) {
                        CommUnit u = (CommUnit) obj;
                        DiscretePoint[] points = PointManager.getInstance().getDiscretePoints(u.getId());
                        java.util.List<DiscretePoint> discretePoints = Arrays.asList(points);
                        Collections.sort(discretePoints, new Comparator<DiscretePoint>() {
                            public int compare(DiscretePoint o1, DiscretePoint o2) {
                                return o1.getName().compareTo(o2.getName());
                            }
                        });
                        for (DiscretePoint p : discretePoints)
                            n.insert(new DefaultMutableTreeNode(p), n.getChildCount());
                        AnalogPoint[] points2 = PointManager.getInstance().getAnalogPoints(u.getId());
                        java.util.List<AnalogPoint> analogPoints = new ArrayList<AnalogPoint>();
                        Collections.addAll(analogPoints, points2);
                        Collections.sort(analogPoints, new Comparator<AnalogPoint>() {
                            public int compare(AnalogPoint o1, AnalogPoint o2) {
                                return o1.toString().compareTo(o2.toString());

                            }
                        });
                        for (AnalogPoint p : analogPoints)
                            n.insert(new DefaultMutableTreeNode(p), n.getChildCount());
                    }
                }
            }
        });
    }

    public static void addPointToTree1(DefaultMutableTreeNode n, final int measureType) {
        TreeUtil.traversalTree_DFS(n, new TreeNodeListener() {
            public void actionPerformed(TreeNode treeNode) {
                if (treeNode.isLeaf() && (treeNode instanceof DefaultMutableTreeNode)) {
                    DefaultMutableTreeNode n = ((DefaultMutableTreeNode) treeNode);
                    Object obj = n.getUserObject();
                    if (obj != null && obj instanceof CommUnit) {
                        CommUnit u = (CommUnit) obj;
                        DiscretePoint[] points = PointManager.getInstance().getDiscretePoints(u.getId());
                        java.util.List<DiscretePoint> discretePoints = Arrays.asList(points);
                        Collections.sort(discretePoints, new Comparator<DiscretePoint>() {
                            public int compare(DiscretePoint o1, DiscretePoint o2) {
                                return o1.getName().compareTo(o2.getName());
                            }
                        });
                        DiscretePoint clone;
                        for (DiscretePoint p : discretePoints) {
                            clone = (DiscretePoint) p.clone();
                            if (p.getMeasureType() == measureType){
                                if(p.getName().contains("-")){
                                    String[] pNames = p.getName().split("-");
                                    clone.setName(pNames[1] + "-" + pNames[2]);
                                }
                                n.insert(new DefaultMutableTreeNode(clone), n.getChildCount());
                            }
                        }
                        AnalogPoint[] points2 = PointManager.getInstance().getAnalogPoints(u.getId());
                        java.util.List<AnalogPoint> analogPoints = new ArrayList<AnalogPoint>();
                        Collections.addAll(analogPoints, points2);
                        Collections.sort(analogPoints, new Comparator<AnalogPoint>() {
                            public int compare(AnalogPoint o1, AnalogPoint o2) {
                                return o1.toString().compareTo(o2.toString());

                            }
                        });
                        AnalogPoint analogClone;
                        for (AnalogPoint p : analogPoints){
                            analogClone = (AnalogPoint) p.clone();
                            if (p.getMeasureType() == measureType){
                                if(p.getName().contains("-")){
                                    String[] pNames = p.getName().split("-");
                                    analogClone.setName(pNames[1] + "-" + pNames[2]);
                                }
                                n.insert(new DefaultMutableTreeNode(analogClone), n.getChildCount());
                            }
                        }
                    }
                }
            }
        });
    }

    public static DefaultTreeModel getDeviceTreeModel(IUnitManager m) {
        MutableTreeNode root = new DefaultMutableTreeNode(m);
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        for (String key : m.getAllUnits().keySet()) {
            Object obj = m.getAllUnits().get(key);
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(obj);
            treeModel.insertNodeInto(node, root, root.getChildCount());
            if (obj instanceof IUnitManager)
                fillTreeNode((IUnitManager) obj, node);
        }
        return treeModel;
    }

    public static DefaultMutableTreeNode getDeviceTreeNode(IUnitManager m) {
        if (m == null)
            return null;
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(m);
        fillTreeNode(m, root);
        return root;
    }

    public static DefaultMutableTreeNode getDeviceTreeNode1(IUnitManager m) {
        if (m == null)
            return null;
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(m);
        fillTreeNode1(m, root);
        return root;
    }

    public static DefaultMutableTreeNode getDeviceTreeNode(IUnitManager m, int maxDepth) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(m);
        fillTreeNode(m, root, maxDepth);
        return root;
    }

    public static void fillTreeNode(IUnitManager m, DefaultMutableTreeNode node) {
        Set<String> strings = m.getAllUnits().keySet();
        java.util.List<String> list = new ArrayList<String>();
        for (String key : strings)
            list.add(key);
        Collections.sort(list);
        for (String aList : list) {
            Object obj = m.getAllUnits().get(aList);
            DefaultMutableTreeNode n = new DefaultMutableTreeNode(obj);
            node.add(n);
            if (obj instanceof IUnitManager)
                fillTreeNode((IUnitManager) obj, n);
        }
//        for (String key : m.getAllUnits().keySet()) {
//            Object obj = m.getAllUnits().get(key);
//            DefaultMutableTreeNode n = new DefaultMutableTreeNode(obj);
//            node.add(n);
//            if (obj instanceof IUnitManager)
//                fillTreeNode((IUnitManager) obj, n);
//        }
    }

    public static void fillTreeNode1(IUnitManager m, DefaultMutableTreeNode node) {
        Set<String> strings = m.getAllUnits().keySet();
        java.util.List<String> list = new ArrayList<String>();
        for (String key : strings)
            list.add(key);
        Collections.sort(list);
        for (String aList : list) {
            Object obj = m.getAllUnits().get(aList);
            if (((CommUnit) obj).getId() == 27 ||((CommUnit) obj).getId() == 34) {
                DefaultMutableTreeNode n = new DefaultMutableTreeNode(obj);
                node.add(n);
                if (obj instanceof IUnitManager)
                    fillTreeNode1((IUnitManager) obj, n);
            }
        }
    }

    public static void fillTreeNode(IUnitManager m, DefaultMutableTreeNode node, int maxDepth) {
        for (String key : m.getAllUnits().keySet()) {
            Object obj = m.getAllUnits().get(key);
            DefaultMutableTreeNode n = new DefaultMutableTreeNode(obj);
            node.add(n);
            if ((obj instanceof IUnitManager) && maxDepth > 1)
                fillTreeNode((IUnitManager) obj, n, maxDepth - 1);
        }
    }


    public static class MyTreeCellRenderer extends DefaultTreeCellRenderer {
        public MyTreeCellRenderer() {
        }

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            JLabel l = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode && ((DefaultMutableTreeNode) value).getUserObject() instanceof CommUnit) {
                l.setIcon(IconOfRTU);
            } else {
                l.setIcon(iconOfFather);
            }
            return l;
        }
    }
}

